﻿/*
 * Authors:    Kęstutis Džiautas
 *             Edmundas Matusevičius
 * University: VU, MIF, SE, 4 course
 * Date:       2010-10-23
 * Version:    1.0
 */

using System;
using System.Collections.Generic;

namespace LexAnalizer {
    public class Tree {
        public Tree() {
            tree = new List<TreeNode>();
        }

        /// <summary>
        /// Returns the count of nodes in the tree.
        /// </summary>
        /// <returns>int number of the nodes in the tree.</returns>
        public int getNodeCount() {
            return tree.Count;
        }

        /// <summary>
        /// Adds node to the tree by provided data.
        /// </summary>
        /// <param name="data">Data object to be added to the node.</param>
        /// <param name="parent">Node object which refers to the parent of the node.</param>
        /// <returns>Node which has been added to the tree.</returns>
        public TreeNode addNode(TreeNodeData data, TreeNode parent) {
            // construct new node
            TreeNode newNode = new TreeNode();
            newNode.setData(data);
            newNode.setParent(parent);

            // add node to the tree
            tree.Add(newNode);

            // return added node
            return newNode;
        }

        /// <summary>
        /// Adds provided node to the tree.
        /// </summary>
        /// <param name="node">Node to be added to the tree.</param>
        /// <returns>Node which has been added to the tree.</returns>
        public TreeNode addNode(TreeNode node) {
            // add node to the tree
            tree.Add(node);

            // return added node
            return node;
        }

        /// <summary>
        /// Returns node by the state's name or null if not found.
        /// </summary>
        /// <param name="state">State to be searched.</param>
        /// <returns>Node which has been searched or null.</returns>
        public TreeNode getNodeByState(string state) {
            // go through all the tree
            foreach (TreeNode node in tree) {
                // search if any node matches the request
                if (node.getData().state.Equals(state)) {
                    // return that node
                    return node;
                }
            }
            // if any node matches the request
            return null;
        }

        /// <summary>
        /// Tree consists of the list of nodes.
        /// </summary>
        private List<TreeNode> tree;
    }
}
